package base.thread;

// 死锁
public class DeadLockDemo {
    public static void main(String[] args) {
        DeadLock deadLock1 = new DeadLock(true);
        DeadLock deadLock2 = new DeadLock(false);
        deadLock1.start();
        deadLock2.start();
    }
}

class DeadLock extends Thread {
    static Object o1 = new Object();
    static Object o2 = new Object();
    boolean flag;

    DeadLock(boolean flag) {
        this.flag = flag;
    }

    @Override
    public void run() {
        if (flag){
            synchronized (o1){
                System.out.println(Thread.currentThread().getName()+"进来了");
                synchronized (o2){
                    System.out.println(Thread.currentThread().getName()+"拿到了o2");
                }
            }
        }else {
            synchronized (o2){
                System.out.println(Thread.currentThread().getName()+"进来了");
                synchronized (o1){
                    System.out.println(Thread.currentThread().getName()+"拿到了o2");
                }
            }
        }

    }
}
